<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信息交互页面</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            height: 100vh;
            background-color: #f5f5f5;
        }
        
        .container {
            display: flex;
            flex-direction: column;
            height: 100%;
            padding: 20px;
            box-sizing: border-box;
        }
        
        .output-box {
            flex: 1;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
            overflow-y: auto;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .input-area {
            display: flex;
            padding: 10px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        #user-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        
        #send-button {
            margin-left: 10px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        
        #send-button:hover {
            background-color: #45a049;
        }
        
        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            border-radius: 4px;
            max-width: 80%;
        }
        
        .user-message {
            background-color: #e3f2fd;
            margin-left: auto;
            text-align: right;
        }
        
        .response-message {
            background-color: #f1f1f1;
            margin-right: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="output-box" id="outputBox">
             <!-- 输出信息将显示在这里  -->
        </div>
        <div class="input-area">
            <input type="text" class="input-field" id="inputField" placeholder="请输入内容...">
            <button class="send-button" id="sendButton">发送</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const outputBox = document.getElementById('outputBox');
            const inputField = document.getElementById('inputField');
            const sendButton = document.getElementById('sendButton');
            
            // 发送消息到CGI并处理响应
            function sendMessage() {
                const message = inputField.value.trim();
                if (message === '') return;
                
                // 显示用户输入
                displayMessage('user', message);
                inputField.value = '';
                
                // 发送到CGI
                fetch('../cgi_main', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: 'message=' + encodeURIComponent(message)
                })
                .then(response => response.text())
                .then(data => {
                    // 显示CGI返回的信息
                    // 1. 显示CGI返回的内容
                    displayMessage('系统', data);
                
                })
                .catch(error => {
                    console.error('Error:', error);
                    displayMessage('system', '发生错误，请稍后再试。');
                });
            }
            
            // 显示消息到输出框
            function displayMessage(sender, message) {
                const messageElement = document.createElement('div');
                messageElement.style.marginBottom = '10px';
                messageElement.style.padding = '8px 12px';
                messageElement.style.borderRadius = '4px';
                
                if (sender === 'user') {
                    messageElement.style.backgroundColor = '#e3f2fd';
                    messageElement.style.textAlign = 'right';
                    messageElement.style.marginLeft = '20%';
                } else {
                    messageElement.style.backgroundColor = '#f1f1f1';
                    messageElement.style.textAlign = 'left';
                    messageElement.style.marginRight = '20%';
                }
                
                messageElement.textContent = message;
                outputBox.appendChild(messageElement);
                outputBox.scrollTop = outputBox.scrollHeight;
            }
            
            // 事件监听
            sendButton.addEventListener('click', sendMessage);
            inputField.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendMessage();
                }
            });
        });
    </script>
</body>
</html>

<!-- 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>输入输出界面</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            height: 100vh;
            background-color: #f5f5f5;
        }
        
        .container {
            display: flex;
            flex-direction: column;
            height: 100%;
            padding: 20px;
            box-sizing: border-box;
        }
        
        .output-box {
            flex: 1;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
            overflow-y: auto;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .input-area {
            display: flex;
            padding: 10px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        #user-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        
        #send-button {
            margin-left: 10px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        
        #send-button:hover {
            background-color: #45a049;
        }
        
        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            border-radius: 4px;
            max-width: 80%;
        }
        
        .user-message {
            background-color: #e3f2fd;
            margin-left: auto;
            text-align: right;
        }
        
        .response-message {
            background-color: #f1f1f1;
            margin-right: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="output-box" id="output-box">
            
        </div>
        
        <div class="input-area">
            <input type="text" id="user-input" placeholder="请输入内容..." autofocus>
            <button id="send-button">发送</button>
        </div>
    </div>

    <script>
        document.getElementById('send-button').addEventListener('click', sendMessage);
        document.getElementById('user-input').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });
        
        function sendMessage() {
            const userInput = document.getElementById('user-input');
            const message = userInput.value.trim();
            
            if (message) {
                // 显示用户消息
                displayMessage(message, 'user-message');
                
                // 清空输入框
                userInput.value = '';
                
                // 这里可以添加AJAX请求将数据发送到后端
                // 示例代码：
                fetch('../cgi_main', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ message: message })
                })
                .then(response => response.json())
                .then(data => {
                    // 显示后端返回的数据
                    displayMessage(data.response, 'response-message');
                })
                // .catch(error => {
                //     displayMessage('发生错误: ' + error, 'response-message');
                // });
                
            }
        }
        
        function displayMessage(text, className) {
            const outputBox = document.getElementById('output-box');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${className}`;
            messageDiv.textContent = text;
            outputBox.appendChild(messageDiv);
            
            // 自动滚动到底部
            outputBox.scrollTop = outputBox.scrollHeight;
        }
    </script>
</body>
</html> -->